#include "Prop.h"
#include <stdlib.h>
#include <stdio.h>

Prop *CreateProp(enum PropType type, void *ptr)
{
    Prop *p=(Prop*)malloc (sizeof(Prop));
    if(p == NULL)
    return NULL;

    p->type=type;
    p->ptr=ptr;
    return p;
    
}

void FreeProp(void *p, void (*clearPtr)(void *))
{
    Prop *pro =(Prop*)p;
    clearPtr(pro->ptr);
    free(p);
}

Weapon *CreateWeapon(const char *name, int attack,enum Effect effect)
{
    Weapon *w=(Weapon*)malloc(sizeof(Weapon));
    if(w==NULL)
    return NULL;
    w->attack=attack;
    w->name=GetString(name);
    w->effect=effect;
    return w;
}

void FreeWeapon(void *ptr)
{
    Weapon *w=(Weapon*)ptr;
    ClearString(&w->name);
    free(w);
}

MyStr WeaponToString(Weapon *w)
{
    char str[1024]={0};
    sprintf(str,"%d %s %d %d\n",WEAPON,w->name.string,w->attack,w->effect);
    return GetString(str);
}

Defence *CreateDefence(const char *name, int defence,enum Effect effect)
{
    Defence *d=(Defence*)malloc(sizeof(Defence));
    if(d==NULL)
    return NULL;
    d->defence=defence;
    d->name=GetString(name);
    d->effect=effect;
    return d;
}

void FreeDefence(void *ptr)
{
    Defence *d=(Defence*)ptr;
    ClearString(&d->name);
    free(d);
}

MyStr DefenceToString(Defence *d)
{
    char str[1024]={0};
    sprintf(str,"%d %s %d %d\n",DEFENCE,d->name.string,d->defence,d->effect);
    return GetString(str);
}

Medicament *CreateMedicament(const char *name,int HP,enum Effect effect)
{
    Medicament *m=(Medicament*)malloc(sizeof(Medicament));
    if(m==NULL)
    return NULL;
    m->HP=HP;
    m->name=GetString(name);
    m->effect=effect;
    return m;
}

void FreeMedicament(void *ptr)
{
    Medicament *m=(Medicament*)ptr;
    ClearString(&m->name);
    free(m);
}

MyStr MedicamentToString(Medicament *m)
{
    char str[1024]={0};
    sprintf(str,"%d %s %d %d\n",MEDICAMENT,m->name.string,m->HP,m->effect);
    return GetString(str);
}
